from abc import ABC
# 不仅仅是列表，其他的可迭代对象也是可以使用推导式的

myList = [i for i in range(1,100+1)]

print(
  myList
)

# 迭代对象
diedai = (i for i in range(1,100+1))

print(diedai)
print(list(diedai))
# print(next(diedai)) # 不知道为什么 这里的代码竟然不能执行。
# print(dict(diedai))

# 对象推导式
dict1 = {
  'name': 'wyl350',
  'age': 34
}

dict2 = {key: str(value)+'1' for key, value in dict1.items()}

print(dict2)

# 元组推导式
tuple1 = (1,2,3)

tuple2 = tuple(value+1 for value in tuple1)

print(tuple2)

# 集合推导式

set1 = {1,2,3}
set2 = {value+10 for value in set1}
print(set2)


# 魔术方法：
# __call__
# __str__
# __getitem__
# __iter__

class Test():
  def __setattr__(self):
    self.name = 'wyl3501'

  def __set__(self, value):
    print('set')
    self.name = value
  
  def __get__(self):
    print('get')
    self.name = 'wyl350'
    return self.name

test1 = Test()

# print(test1.name)


class MyABC(ABC):
  pass

MyABC.register(tuple)

assert issubclass(tuple, MyABC)
assert isinstance((), MyABC)

# 数据库实例化的库： pip install SQLalchemy

class Singleton():
  def __new__(cls):
    if not hasattr(cls, 'instance'):
      print('if')
      cls.instance = super().__new__(cls)
    return cls.instance

s1 = Singleton()
s2 = Singleton()
Singleton()
Singleton()
Singleton()

print(
  id(s1) == id(s2),
)
